package com.swkl.yuan.agvtask.dataController;

import cn.hutool.extra.spring.SpringUtil;
import com.swkl.yuan.agvtask.AgvTaskApplication;
import com.swkl.yuan.agvtask.task.MainTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RestartController {

    @Autowired
    private ApplicationContext context;

    @GetMapping("/restart")
    public String restart() {
        Thread restartThread = new Thread(() -> {
            try {
                Thread.sleep(700);
                //关闭当前
                SpringApplication.exit(context, () -> 0);
                //重新启动
                SpringApplication.run(AgvTaskApplication.class);
                SpringUtil.getBean(MainTask.class).init();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        });
        restartThread.setDaemon(false);
        restartThread.start();
        return "应用程序正在重新重新应用...";
    }
}
